package com.xmut.mall.search.controller;

import com.xmut.mall.search.annotation.CheckJwt;
import com.xmut.mall.search.enums.product.BizCodeEnum;
import com.xmut.mall.search.es.document.product.ProductESPojo;
import com.xmut.mall.search.service.ProductService;
import com.xmut.mall.search.utils.R;
import com.xmut.mall.search.utils.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/search/save")
@RestController
public class EsSaveController {

    @Autowired
    private ProductService productService;

    @PostMapping("/product")
    public R productUp(@RequestBody List<ProductESPojo> skuEsModels){
        boolean result = false;

        try {
            result = productService.productUp(skuEsModels);
        } catch (Exception e) {
            log.error("ES商品上架失败:{}",e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }

        if(result){
            return R.ok();
        }else {
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
    }

    /**
     * 商品下架
     * @param skuIds
     * @return
     */
    @PostMapping("/productOFF")
    public R productOFF(@RequestBody List<Long> skuIds){
        boolean result = false;

        try {
            result = productService.productOFF(skuIds);
        } catch (Exception e) {
            log.error("ES商品上架失败:{}",e);
            return R.error(BizCodeEnum.PRODUCT_OFF_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_OFF_EXCEPTION.getMsg());
        }

        if(result){
            return R.ok();
        }else {
            return R.error(BizCodeEnum.PRODUCT_OFF_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_OFF_EXCEPTION.getMsg());
        }
    }

    /**
     * 快捷重置文档接口
     * @return
     */
    @GetMapping("/deleteDocument")
    public RespBean deleteDocument(){
        return productService.deleteDocument();
    }
}
